/*
 * jPhoneMicro.java
 *
 * Created on Mar 22, 2010, 6:31:03 PM
 *
 * @author pquiring@gmail.com
 *
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

import javaforce.*;
import javaforce.voip.*;

/** jphonelite Panel */

public class PhoneMicro extends BasePhone implements MeterController, GUI {

  public String number = "";  //forced number to always dial

  /** Creates new form jPhoneMicro */
  public PhoneMicro(WindowController wc, String number) {
    try {
      this.number = number;
      initBasePhone(this, wc);
      Settings.isApplet = true;
      initComponents();
      init();
      //must set focus to something or VK_BACK_SPACE beeps
      SwingUtilities.invokeLater(new Runnable(){
        public void run(){
          call.requestFocusInWindow();  //must be called from EDT or when window is visible
        }
      });
    } catch (Exception e) {
      JFLog.log("Error:" + e);
    }
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        shareDesktopMenu = new javax.swing.JPopupMenu();
        shareDesktopViewOnly = new javax.swing.JMenuItem();
        shareDesktopRemoteControl = new javax.swing.JMenuItem();
        call = new javax.swing.JToggleButton();
        end = new javax.swing.JToggleButton();
        status = new javax.swing.JTextField();
        logo = new javax.swing.JLabel();

        shareDesktopViewOnly.setText("Share Desktop (view only)");
        shareDesktopMenu.add(shareDesktopViewOnly);

        shareDesktopRemoteControl.setText("Share Desktop (remote control)");
        shareDesktopMenu.add(shareDesktopRemoteControl);

        setLayout(null);

        call.setText("Call");
        call.setFocusCycleRoot(true);
        call.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                callActionPerformed(evt);
            }
        });
        add(call);
        call.setBounds(10, 40, 70, 50);

        end.setText("End");
        end.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                endActionPerformed(evt);
            }
        });
        add(end);
        end.setBounds(210, 40, 70, 50);

        status.setEditable(false);
        add(status);
        status.setBounds(10, 10, 270, 20);

        logo.setText(".");
        add(logo);
        logo.setBounds(80, 30, 130, 70);
    }// </editor-fold>//GEN-END:initComponents

    private void callActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_callActionPerformed
      selectLine(0);
      lines[0].dial = number;
      call();
    }//GEN-LAST:event_callActionPerformed

    private void endActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_endActionPerformed
      end();
    }//GEN-LAST:event_endActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton call;
    private javax.swing.JToggleButton end;
    private javax.swing.JLabel logo;
    private javax.swing.JPopupMenu shareDesktopMenu;
    private javax.swing.JMenuItem shareDesktopRemoteControl;
    private javax.swing.JMenuItem shareDesktopViewOnly;
    private javax.swing.JTextField status;
    // End of variables declaration//GEN-END:variables

  /** Init panel variables. */

  public void init() {
    loadIcons();
    Settings.loadSettings();
    setFonts();
    wc.setPanelAlwaysOnTop(Settings.current.alwaysOnTop);
    JFLog.setEnabled(!Settings.current.disableLogging);
    JFLog.log("jphonelite/" + version);

    loadingConfig = true;
    updateContactList();
    loadingConfig = false;
    updateRecentList();
    lines = new PhoneLine[6];
    for(int a=0;a<6;a++) {
      lines[a] = new PhoneLine();
    }

    //set PICs

    call.setText("");
    call.setIcon(ii[PIC_CALL]);
//    call.setPressedIcon(ii[PIC_CALL2]);
    call.setSelectedIcon(ii[PIC_CALL2]);

    end.setText("");
    end.setIcon(ii[PIC_END]);
//    end.setPressedIcon(ii[PIC_END2]);
    end.setSelectedIcon(ii[PIC_END2]);

    logo.setText("");
    logo.setIcon(ii[PIC_LOGO]);

    keepAliveinit();
    boolean sound_init = sound.init(lines, this);
    setScales();  //requires sound.init()
    new Thread() {
      public void run() {
        reRegisterAll();
      }
    }.start();
    if (!sound_init) {
      JOptionPane.showMessageDialog(this,
        "No compatible sound found",
        "Warning",
        JOptionPane.INFORMATION_MESSAGE);
    }
  }

  /** MeterController : Sets the recording level on the display meter. */

  public void setMeterRec(int lvl) {}

  /** MeterController : Sets the playback level on the display meter. */

  public void setMeterPlay(int lvl) {}

  /** MeterController : Sets the speaker mic status (green=true red=false). */

  public void setSpeakerStatus(boolean state) {
  }

  public void setScales() {
    if (sound.isSWVolRec()) {
      sound.setVolRec(75);
    } else {
      sound.setVolRec(100);
    }
    if (sound.isSWVolPlay()) {
      sound.setVolPlay(75);
    } else {
      sound.setVolPlay(100);
    }
  }

  /** Select a new line. */

  public void selectLine(int newline) {
    //make sure line is valid
    if ((line != -1) && (lines[line].dtmf != 'x')) lines[line].dtmfend = true;
    line = newline;
    if (line == -1) {
      status.setText("");
      return;
    }
    sound.selectLine(line);
    updateLine();
  }

  /** Update the number to be dialed, status and buttons for current selected line. */

  public void updateLine() {
    end.setSelected(false);
    if (lines[line].incall) {
      call.setSelected(true);
    } else {
      call.setSelected(false);
    }
    status.setText(lines[line].status);
  }

  public void updateCallButton(boolean state) {
    call.setSelected(state);
  }

  public void updateEndButton(boolean state) {
    end.setSelected(state);
  }

  public void endLineUpdate(int xline) {
  }

  public void callInviteUpdate() {
  }

  /** Sets a contacts icon status when received from a NOTIFY message. */

  public void setStatus(String number, String server, String status) {
  }

  /** Updates contact list after a change. */

  public void updateContactList() {
  }

  /** Update recent lists after a change. */

  public void updateRecentList() {
  }

  public void hld_setIcon(ImageIcon ii) {
  }

  public void aa_setIcon(ImageIcon ii) {
  }

  public void ac_setIcon(ImageIcon ii) {
  }

  public void dnd_setIcon(ImageIcon ii) {
  }

  public void cnf_setIcon(ImageIcon ii) {
  }

  public void mute_setIcon(ImageIcon ii) {
  }

  public void spk_setIcon(ImageIcon ii) {
  }

  public void setFonts() {
//    JFLog.log("Font name= " + l1.getFont().getName() + ",stype=" + l1.getFont().getStyle() + ",size=" + l1.getFont().getSize() );
    //sets font sizes for all buttons
    int small = 10;
    int large = 12;  //default size
    if (Settings.current.smallerFont) {
      small -= 2;
      large -= 2;
    }
    Font smallFont = new java.awt.Font("Dialog", 0, small);
    Font largeFont = new java.awt.Font("Dialog", 0, large);
  }

  public void onRegister(SIPClient sip) {
  }

  public void rtp_jpeg_receive(byte data[], int pos, int len) {}

  public String getLineStatus() {
    return status.getText();
  }

}
